package cn.ac.jaas.ljx.spring.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class DemoAspect {

	@Pointcut("@annotation(cn.ac.jaas.ljx.spring.aop.DemoAction)")
	public void annotationPointcut() {};
	@Pointcut("execution(* cn.ac.jaas.ljx.spring.aop.DemoMethodService.add(..))")
	public void methodPointcut() {};
	
	@After("annotationPointcut()")
	public void after(JoinPoint joinPoint) {
		MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
		Object[] objs = joinPoint.getArgs();
		String string = (String)objs[0];
 		System.out.println(string.substring(0, 1));
 		System.out.println(objs[1]);
 		Object obj = joinPoint.getTarget();
 		System.out.println(obj);
		Method method = methodSignature.getMethod();
		//System.out.println(method.getName());
		DemoAction action = method.getAnnotation(DemoAction.class);
		//System.out.println(action.name());
	}
	
	@Before("methodPointcut()")
	public void before(JoinPoint joinPoint) {
		MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
		Method method = methodSignature.getMethod();
		System.out.println("基于方法规则的aop, 拦截的方法：" + method.getName());
	}
}
